package com.example.viewtest.animate_7;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.viewtest.R;
import com.example.viewtest.Utils;

/**
 * 翻页动画
 */
public class RotationAnimateView extends View {
    private static final String TAG = "RotationAnimateView";
    private static final float START_ROTATE = -90;

    private Camera camera = new Camera();
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap bitmap;

    private RectF viewRange = new RectF();
    private RectF endRect = new RectF();
    private RectF startRect = new RectF();

    private float offsetAngle = 0;
    private float rotateStartX = 0;
    private float rotateEndX = 0;

    private Matrix matrix = new Matrix();

    private AnimatorSet animatorSet = new AnimatorSet();

    public RotationAnimateView(Context context) {
        super(context);
    }

    public RotationAnimateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RotationAnimateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        long l = System.currentTimeMillis();
        bitmap = Utils.getBitmap(getResources(), R.mipmap.head, (int) Utils.db2px(300));
        camera.setLocation(0, 0, Utils.getCameraZ());


        ObjectAnimator rotateStartXAnimator = ObjectAnimator.ofFloat(this, "rotateStartX", 45).setDuration(300);
        ObjectAnimator retateZAnimator = ObjectAnimator.ofFloat(this, "offsetAngle", -270).setDuration(1500);
        ObjectAnimator rotateEndXAnimator = ObjectAnimator.ofFloat(this, "rotateEndX", -45).setDuration(300);
        animatorSet.playSequentially(rotateStartXAnimator, retateZAnimator, rotateEndXAnimator);
        Log.e(TAG, "instance initializer: " + (System.currentTimeMillis() - l));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        long l = System.currentTimeMillis();
        viewRange.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
        endRect.set(viewRange.left, viewRange.centerY(), viewRange.right, viewRange.bottom);
        startRect.set(viewRange.left, viewRange.top, viewRange.right, viewRange.centerY());
        Log.e(TAG, "onSizeChanged: " + (System.currentTimeMillis() - l));
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // animatorSet.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        long l = System.currentTimeMillis();
        float degrees = START_ROTATE + offsetAngle;
        //绘制初始翻起的一半
        drawHalfPicture(canvas, degrees, rotateStartX, endRect);

        //绘制最后翻起的一半
        drawHalfPicture(canvas, degrees, rotateEndX, startRect);
        Log.e(TAG, "onDraw: " + (System.currentTimeMillis() - l));
    }

    /**
     * 绘制一半图片
     *
     * @param canvas       canvas
     * @param rotateAngle  旋转角度
     * @param rotateXAngle X轴旋转角度
     * @param clip         需要截掉的矩形
     */
    private void drawHalfPicture(Canvas canvas, float rotateAngle, float rotateXAngle, RectF clip) {
        canvas.save();
        //旋转画图
        canvas.rotate(rotateAngle, viewRange.centerX(), viewRange.centerY());

        //X轴旋转
      /*  camera.save();
        camera.rotateX(rotateXAngle);
        canvas.translate((getWidth() / 2), (getHeight() / 2));
        camera.applyToCanvas(canvas);
        canvas.translate(-(getWidth() / 2), -(getHeight() / 2));
        camera.restore();*/
        set3DMetrix(rotateXAngle);
        canvas.concat(matrix);

        //截掉不绘制的部分
        canvas.clipRect(clip);
        //恢复旋转角度
        canvas.rotate(-rotateAngle, viewRange.centerX(), viewRange.centerY());

        canvas.drawBitmap(bitmap, viewRange.centerX() - bitmap.getWidth() / 2, viewRange.centerY() - bitmap.getHeight() / 2, paint);

        canvas.restore();
    }

    private void set3DMetrix(float rotateXAngle) {
        matrix.reset();

        camera.save();
        camera.rotateX(rotateXAngle);
        camera.getMatrix(matrix);
        camera.restore();
        //camera默认旋转是View左上角为旋转中心
        //所以动作之前要，设置矩阵位置 -mTextHeight-mOutSideRadius
        matrix.preTranslate(-getWidth() / 2, -getHeight() / 2);
        //动作之后恢复位置
        matrix.postTranslate(getWidth() / 2, getHeight() / 2);
    }

    public void startAnimation() {
        if (!animatorSet.isRunning())
            animatorSet.start();
    }

    public float getOffsetAngle() {
        return offsetAngle;
    }

    public void setOffsetAngle(float offsetAngle) {
        this.offsetAngle = offsetAngle;
        invalidate();
    }

    public float getRotateStartX() {
        return rotateStartX;
    }

    public void setRotateStartX(float rotateStartX) {
        this.rotateStartX = rotateStartX;
        invalidate();
    }


    public float getRotateEndX() {
        return rotateEndX;
    }

    public void setRotateEndX(float rotateEndX) {
        this.rotateEndX = rotateEndX;
        invalidate();
    }
}
